// pages/advice/advice.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    adv:'',
    isDisabled:true

  },
  sr:function(event){
    this.setData({
      adv:event.detail.value
    });
  },
  tj:function(){
   var a = this.data.adv;
   var openId = wx.getStorageSync('openId');
   wx.cloud.callFunction({
    name: "advicesLimit",
    data: {
      _openid:openId
    },
    complete: res => {
      var record = res.result
      if (record > 1){
        wx.showToast({
          title: '请明天再提！',
        })
        this.setData({
          isDisabled:false
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '../home/home',
          })
        },1200)
      }else{
        if (!a){
          wx.showToast({
            title: '不能为空！',
          })
        } else {
         db.collection('advices').add({
           data:{
             advice:a,
             time:db.serverDate()
           },
           success: function () {
             wx.showToast({
               title: '提交成功,谢谢！',
               icon: "success",
               duration: 800
             })
             setTimeout(()=>{
             wx.switchTab({
               url: '../home/home',
             })
           },1200)
           },
           fail:function(){
             wx.showToast({
               title: '提交失败！',
               icon: "error",
               duration: 800
             })
           }   
         })
       }
      }
    }
  })
  },
  remove:function(){
    wx.removeStorage({
      key: 'list',
      success (res) {
        wx.showToast({
          title: '清除首页商品信息成功',
        })
      }
    });
    wx.removeStorage({
      key: 'store',
      success (res) {
        wx.showToast({
          title: '更新自提点！',
        })
      }
    })
  }
})